#include "../../ui.h"
#include "../../ui_helpers.h"
#include "../Inc/ui_HomePage.h"
#include "../Inc/ui_MenuPage.h"
#include "../Inc/ui_Megboxes.h"
#include "../Inc/ui_CalendarPage.h"
#include "../Inc/ui_ComputerPage.h"
#include "../Inc/ui_TimerPage.h"
#include "../Inc/ui_NFCCardPage.h"
//#include "ui_SprotsPage.h"
#include "../Inc/ui_HRPage.h"
#include "../Inc/ui_SPO2Page.h"
#include "../Inc/ui_EnvPage.h"
#include "../Inc/ui_CompassPage.h"
#include "../Inc/ui_GameSelectPage.h"
#include "../Inc/ui_SetPage.h"
#include "../Inc/ui_AboutPage.h"

typedef void (*ui_event_func_ptr)(lv_event_t *);

void ui_event_MenuCntPanel(lv_event_t * e);

///////////////////// Page Manager //////////////////
Page_t Page_Menu = {ui_MenuPage_screen_init, ui_MenuPage_screen_deinit, &ui_MenuPage};

///////////////////// VARIABLES ////////////////////
//menu page
static lv_img_dsc_t *ui_img_dsc_menu[XT_MENU_ICON_MAX] = {NULL};
static char ui_menu_icon[XT_MENU_ICON_MAX][64] = {
    {"set_icon_missing.png"},
    {"set_icon_scene_standard.png"},
    {"set_icon_brightness.png"},
    {"set_icon_image_contrast.png"},
    {"set_icon_shutter.png"},
    {"set_icon_palette.png"},
    {"icon_image_auto_poweroff.png"},
    {"set_icon_time.png"},
    {"set_icon_lang.png"},
    {"set_icon_photo.png"},
    {"set_icon_hostpot.png"},
    {"set_icon_div_line.png"},
    {"set_icon_bad_pixel.png"},
    {"index_icon_wifi_nor.png"},
    {"set_icon_reste.png"},
    {"set_icon_info.png"},
};

static char ui_menu_label[XT_MENU_ICON_MAX][32] = {
    {"Missing"},
    {"Scene Mode"},
    {"Brightness"},
    {"Contract"},
    {"Calibration"},
    {"Palette"},
    {"Shutdown"},
    {"Date/Time"},
    {"Language"},
    {"Capture"},
    {"Hotspot"},
    {"Reticle"},
    {"Correction"},
    {"WiFi"},
    {"Reset"},
    {"About"},
};

static ui_event_func_ptr ui_event_ptrs[XT_MENU_ICON_MAX] = {
    ui_event_MenuCntPanel,
    ui_event_MenuCntPanel,
    ui_event_MenuCntPanel,
    ui_event_MenuCntPanel,
    ui_event_MenuCntPanel,
    ui_event_MenuCntPanel,
    ui_event_MenuCntPanel,
    ui_event_MenuCntPanel,
    ui_event_MenuCntPanel,
    ui_event_MenuCntPanel,
    ui_event_MenuCntPanel,
    ui_event_MenuCntPanel,
    ui_event_MenuCntPanel,
    ui_event_MenuCntPanel,
    ui_event_MenuCntPanel,
};

s_xt_menu_item xt_settings[XT_MENU_ICON_MAX];

lv_obj_t * ui_MenuPage;

lv_obj_t * ui_MenuCalPanel;
lv_obj_t * ui_MenuCalButton;
lv_obj_t * ui_MenuCalicon;
lv_obj_t * ui_MenuCalLabel;

lv_obj_t * ui_MenuComPanel;
lv_obj_t * ui_MenuComButton;
lv_obj_t * ui_MenuComicon;
lv_obj_t * ui_MenuComLabel;

lv_obj_t * ui_MenuTimPanel;
lv_obj_t * ui_MenuTimButton;
lv_obj_t * ui_MenuTimicon;
lv_obj_t * ui_MenuTimLabel;

lv_obj_t * ui_MenuCardPanel;
lv_obj_t * ui_MenuCardButton;
lv_obj_t * ui_MenuCardicon;
lv_obj_t * ui_MenuCardLabel;

lv_obj_t * ui_MenuSprPanel;
lv_obj_t * ui_MenuSprButton;
lv_obj_t * ui_MenuSpricon;
lv_obj_t * ui_MenuSprLabel;

lv_obj_t * ui_MenuHRPanel;
lv_obj_t * ui_MenuHRButton;
lv_obj_t * ui_MenuHRicon;
lv_obj_t * ui_MenuHRLabel;

lv_obj_t * ui_MenuO2Panel;
lv_obj_t * ui_MenuO2Button;
lv_obj_t * ui_MenuO2icon;
lv_obj_t * ui_MenuO2Label;

lv_obj_t * ui_MenuEnvPanel;
lv_obj_t * ui_MenuEnvButton;
lv_obj_t * ui_MenuEnvicon;
lv_obj_t * ui_MenuEnvLabel;

lv_obj_t * ui_MenuCPPanel;
lv_obj_t * ui_MenuCPButton;
lv_obj_t * ui_MenuCPicon;
lv_obj_t * ui_MenuCPLabel;

lv_obj_t * ui_MenuGamePanel;
lv_obj_t * ui_MenuGameButton;
lv_obj_t * ui_MenuGameicon;
lv_obj_t * ui_MenuGameLabel;

lv_obj_t * ui_MenuSetPanel;
lv_obj_t * ui_MenuSetButton;
lv_obj_t * ui_MenuSeticon;
lv_obj_t * ui_MenuSetLabel;

lv_obj_t * ui_MenuAbPanel;
lv_obj_t * ui_MenuAbButton;
lv_obj_t * ui_MenuAbicon;
lv_obj_t * ui_MenuAbLabel;


//scroll y
int16_t ui_MenuScrollY = 0;


///////////////////// FUNCTIONS ////////////////////
static void ui_setting_release_res(void) {
    for (int i_cnt = 0; i_cnt < XT_MENU_ICON_MAX; i_cnt++) {
        release_resource(&ui_img_dsc_menu[i_cnt]);
    }

}

static void ui_setting_load_res(void) {
    ui_setting_release_res();
    char file_str[128] = {0};
    lv_img_dsc_t *p_img = NULL, *p_img_missing = NULL;
    snprintf(file_str, 128, "%s%s", UI_RESOURCE_PATH, ui_menu_icon[XT_MENU_ICON_MISSING]);
    file_str[127] = '\0';
    ui_img_dsc_menu[XT_MENU_ICON_MISSING] = p_img_missing = load_image_resource(file_str);
    for(int i_cnt = 1; i_cnt < XT_MENU_ICON_MAX; i_cnt++) {
        xt_prt("[%d]:%s\n", i_cnt, ui_menu_icon[i_cnt]);
        snprintf(file_str, 128, "%s%s", UI_RESOURCE_PATH, ui_menu_icon[i_cnt]);
        file_str[127] = '\0';
        ui_img_dsc_menu[i_cnt] = p_img = load_image_resource(file_str);
        if(p_img == NULL) {
            xt_prt("[%d]:%s, used the missing img\n", i_cnt, ui_menu_icon[i_cnt]);
            p_img = p_img_missing;
        }
    }
}

void ui_event_MenuPage(lv_event_t * e) {
   lv_event_code_t event_code = lv_event_get_code(e);
    ui_event_BackPage(e);
    if(event_code == LV_EVENT_SCROLL_END) {
        ui_MenuScrollY = lv_obj_get_scroll_y(ui_MenuPage);
        xt_prt("ui_MenuScrollY:%d:%x\n", ui_MenuScrollY, ui_MenuScrollY);
    }
}


void ui_event_MenuCalPanel(lv_event_t * e) {
    lv_event_code_t event_code = lv_event_get_code(e);
    lv_obj_t * target = lv_event_get_target(e);
    e->user_data = (void*)(&Page_Calender);
    ui_event_BackPage(e);
    if(event_code == LV_EVENT_CLICKED) {
#if CALENDAR_PAGE_EN
        Page_Load(&Page_Calender);
#else


#endif
    }
}

void ui_event_MenuComPanel(lv_event_t * e) {
    lv_event_code_t event_code = lv_event_get_code(e);
    lv_obj_t * target = lv_event_get_target(e);
    e->user_data = (void*)(&Page_Computer);
    ui_event_BackPage(e);
    if(event_code == LV_EVENT_CLICKED) {
#if COMPUTER_PAGE_EN
        Page_Load(&Page_Computer);
#else


#endif

    }
}

void ui_event_MenuTimPanel(lv_event_t * e) {
    lv_event_code_t event_code = lv_event_get_code(e);
    lv_obj_t * target = lv_event_get_target(e);
    xt_prt("event_code:%d:%x\n", event_code, event_code);
    e->user_data = (void*)(&Page_Timer);
    ui_event_BackPage(e);
    if(event_code == LV_EVENT_CLICKED) {
#if TIMER_PAGE_EN
        Page_Load(&Page_Timer);
#else


#endif
    }
}

void ui_event_MenuCardPanel(lv_event_t * e) {
    lv_event_code_t event_code = lv_event_get_code(e);
    lv_obj_t * target = lv_event_get_target(e);
    e->user_data = (void*)(&Page_NFCCard);
    ui_event_BackPage(e);
    if(event_code == LV_EVENT_CLICKED) {
        #if CARD_PAGE_EN
            Page_Load(&Page_NFCCard);
        #else


        #endif

    }
}

void ui_event_MenuSprPanel(lv_event_t * e) {
    lv_event_code_t event_code = lv_event_get_code(e);
    lv_obj_t * target = lv_event_get_target(e);
    e->user_data = NULL;
    ui_event_BackPage(e);
    if(event_code == LV_EVENT_CLICKED) {
        #if SPROTS_PAGE_EN
        ui_mbox_create("开发中");
        #else


        #endif
    }
}

void ui_event_MenuCntPanel(lv_event_t * e) {
    lv_event_code_t event_code = lv_event_get_code(e);
    lv_obj_t * target = lv_event_get_target(e);
    e->user_data = NULL;
    ui_event_BackPage(e);
    if(event_code == LV_EVENT_CLICKED) {
        #if SPROTS_PAGE_EN
        ui_mbox_create("开发中");
        #else


        #endif
    }
}

void ui_event_MenuHRPanel(lv_event_t * e) {
    lv_event_code_t event_code = lv_event_get_code(e);
    lv_obj_t * target = lv_event_get_target(e);
    e->user_data = (void*)(&Page_HR);
    ui_event_BackPage(e);
    if(event_code == LV_EVENT_CLICKED) {
    #if HR_PAGE_EN
        Page_Load(&Page_HR);
        //send the sensor wakeup message

    #else


    #endif
    }
}

void ui_event_MenuO2Panel(lv_event_t * e) {
    lv_event_code_t event_code = lv_event_get_code(e);
    lv_obj_t * target = lv_event_get_target(e);
    e->user_data = (void*)(&Page_SPO2);
    ui_event_BackPage(e);
    if(event_code == LV_EVENT_CLICKED) {
        #if O2_PAGE_EN
            Page_Load(&Page_SPO2);
        #else


        #endif
    }
}

void ui_event_MenuEnvPanel(lv_event_t * e) {
    lv_event_code_t event_code = lv_event_get_code(e);
    lv_obj_t * target = lv_event_get_target(e);
    e->user_data = (void*)(&Page_Env);
    ui_event_BackPage(e);
    if(event_code == LV_EVENT_CLICKED) {
    #if ENVIRONMENT_PAGE_EN
        Page_Load(&Page_Env);
    #else


    #endif
    }
}

void ui_event_MenuCPPanel(lv_event_t * e) {
    lv_event_code_t event_code = lv_event_get_code(e);
    lv_obj_t * target = lv_event_get_target(e);
    e->user_data = (void*)(&Page_Compass);
    ui_event_BackPage(e);
    if(event_code == LV_EVENT_CLICKED) {
    #if COMPASS_PAGE_EN
        Page_Load(&Page_Compass);
    #else


    #endif
    }
}

void ui_event_MenuGamePanel(lv_event_t * e) {
    lv_event_code_t event_code = lv_event_get_code(e);
    lv_obj_t * target = lv_event_get_target(e);
    e->user_data = (void*)(&Page_GameSelect);
    ui_event_BackPage(e);
    if(event_code == LV_EVENT_CLICKED) {
    #if GAME_PAGE_EN
        Page_Load(&Page_GameSelect);
    #else


    #endif

    }
}

void ui_event_MenuSetPanel(lv_event_t * e) {
    lv_event_code_t event_code = lv_event_get_code(e);
    lv_obj_t * target = lv_event_get_target(e);
    e->user_data = (void*)(&Page_Set);
    ui_event_BackPage(e);
    if(event_code == LV_EVENT_CLICKED) {
    #if SET_PAGE_EN
        Page_Load(&Page_Set);
    #else


    #endif

    }
}

void ui_event_MenuAbPanel(lv_event_t * e) {
    lv_event_code_t event_code = lv_event_get_code(e);
    lv_obj_t * target = lv_event_get_target(e);
    e->user_data = (void*)(&Page_About);
    ui_event_BackPage(e);
    if(event_code == LV_EVENT_CLICKED) {
    #if ABOUT_PAGE_EN
        Page_Load(&Page_About);
    #else


    #endif
    }
}

///////////////////// SCREEN init ////////////////////
void ui_MenuPage_screen_init(void) {
    ui_MenuPage = lv_obj_create(NULL);
    groupRect = lv_group_create();
    lv_indev_set_group(indev_keypad, groupRect);
    ui_setting_load_res();
    int menu_icon_pixel = 80, menu_icon_x = -15, menu_icon_y = 0;
    lv_obj_t * p_tmp_obj = NULL;
    lv_obj_scroll_to(ui_MenuPage,0,ui_MenuScrollY,LV_ANIM_OFF);
    lv_obj_add_event_cb(ui_MenuPage, ui_event_MenuPage, LV_EVENT_ALL, NULL);
    for(int i_cnt = 0; i_cnt < XT_MENU_ICON_MAX-1; i_cnt++) {
        xt_settings[i_cnt].Panel = p_tmp_obj = lv_obj_create(ui_MenuPage);
        lv_obj_set_width(p_tmp_obj, menu_icon_pixel*2);
        lv_obj_set_height(p_tmp_obj, menu_icon_pixel);
        lv_obj_set_x(p_tmp_obj, menu_icon_x);
        lv_obj_set_y(p_tmp_obj, menu_icon_y); menu_icon_y += menu_icon_pixel;
        lv_obj_set_align(p_tmp_obj, LV_ALIGN_TOP_LEFT);
        lv_obj_clear_flag(p_tmp_obj, LV_OBJ_FLAG_SCROLLABLE);      /// Flags
        lv_obj_set_style_radius(p_tmp_obj, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
        lv_obj_set_style_bg_color(p_tmp_obj, lv_color_hex(0xFFFFFF), LV_PART_MAIN | LV_STATE_DEFAULT);
        lv_obj_set_style_bg_opa(p_tmp_obj, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
        lv_obj_set_style_border_color(p_tmp_obj, lv_color_hex(0x000000), LV_PART_MAIN | LV_STATE_DEFAULT);
        lv_obj_set_style_border_opa(p_tmp_obj, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
        lv_obj_set_style_bg_color(p_tmp_obj, lv_color_hex(0x808080), LV_PART_MAIN | LV_STATE_PRESSED);
        lv_obj_set_style_bg_opa(p_tmp_obj, 100, LV_PART_MAIN | LV_STATE_PRESSED);
        
        xt_settings[i_cnt].Button = p_tmp_obj = lv_btn_create(xt_settings[i_cnt].Panel);
        lv_obj_set_width(p_tmp_obj, menu_icon_pixel);
        lv_obj_set_height(p_tmp_obj, menu_icon_pixel);
        lv_obj_set_align(p_tmp_obj, LV_ALIGN_LEFT_MID);
        lv_obj_add_flag(p_tmp_obj, LV_OBJ_FLAG_SCROLL_ON_FOCUS);     /// Flags
        lv_obj_clear_flag(p_tmp_obj, LV_OBJ_FLAG_CLICKABLE | LV_OBJ_FLAG_SCROLLABLE);      /// Flags
        lv_obj_set_style_radius(p_tmp_obj, menu_icon_pixel, LV_PART_MAIN | LV_STATE_DEFAULT);
        lv_obj_set_style_bg_color(p_tmp_obj, lv_color_hex(0xFF8080), LV_PART_MAIN | LV_STATE_DEFAULT);
        lv_obj_set_style_bg_opa(p_tmp_obj, 255, LV_PART_MAIN | LV_STATE_DEFAULT);
        
        xt_settings[i_cnt].icon = p_tmp_obj = lv_img_create(xt_settings[i_cnt].Button);
        lv_obj_set_x(p_tmp_obj, -10);
        lv_obj_set_y(p_tmp_obj, -5);
        lv_img_set_src(p_tmp_obj, ui_img_dsc_menu[i_cnt+1]);
        
        xt_settings[i_cnt].Label = p_tmp_obj = lv_label_create(xt_settings[i_cnt].Panel);
        lv_obj_set_width(p_tmp_obj, LV_SIZE_CONTENT);   /// 1
        lv_obj_set_height(p_tmp_obj, LV_SIZE_CONTENT);    /// 1
        lv_obj_set_x(p_tmp_obj, 0);
        lv_obj_set_y(p_tmp_obj, 0);
        lv_obj_set_align(p_tmp_obj, LV_ALIGN_LEFT_MID);
        lv_label_set_text(p_tmp_obj, ui_menu_label[i_cnt+1]);
        lv_obj_set_style_text_color(p_tmp_obj, lv_color_hex(COLOR_BLUE), 0);
        lv_obj_set_style_text_font(p_tmp_obj, ttf_info_12.font, LV_PART_MAIN | LV_STATE_DEFAULT);
        
        lv_obj_add_event_cb(xt_settings[i_cnt].Panel, ui_event_ptrs[i_cnt], LV_EVENT_ALL, NULL);
        lv_obj_add_event_cb(xt_settings[i_cnt].Button, ui_event_ptrs[i_cnt], LV_EVENT_ALL, NULL);
        lv_group_add_obj(groupRect, xt_settings[i_cnt].Button);
    }
}

/////////////////// SCREEN deinit ////////////////////
void ui_MenuPage_screen_deinit(void) {}
